package com.kust.smarthome.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kust.smarthome.web.mapper.UserAddressMapper;
import com.kust.smarthome.web.model.UserAddress;
import com.kust.smarthome.web.service.IUserAddressService;
import com.kust.smarthome.web.vo.UserAddressVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author iAuzre
 * @since 2021-07-09
 */
@Service
public class UserAddressServiceImpl extends ServiceImpl<UserAddressMapper, UserAddress> implements IUserAddressService {

    @Autowired
    private UserAddressMapper userAddressMapper;

    @Override
    public int insert(UserAddress userAddress) {
        return userAddressMapper.insert(userAddress);
    }

    @Override
    public List<UserAddress> selectByUserId(Integer userId) {

        QueryWrapper<UserAddress> query = new QueryWrapper<>();
        query.eq("user_id", userId);

        return userAddressMapper.selectList(query);
    }

    @Override
    public String updateUserAddress(Integer userId, UserAddressVo userAddressVo) {
        /*name phone province municipality district addressIntro*/

        UserAddress userAddress = userAddressMapper.selectById(userAddressVo.getId());
        if (userAddress.getUserId() == userId) {
            //导入更新数据
            userAddress.setName(userAddressVo.getName())
                    .setPhone(userAddressVo.getPhone())
                    .setProvince(userAddressVo.getProvince())
                    .setMunicipality(userAddressVo.getMunicipality())
                    .setDistrict(userAddressVo.getDistrict())
                    .setAddressIntro(userAddressVo.getAddressIntro());

            userAddressMapper.updateById(userAddress);
            return "修改成功";
        }

        return "非本人无法修改";
    }

    @Override
    @Transactional
    public boolean absoluteAddress(Integer userId, Integer id) {
        QueryWrapper<UserAddress> query = new QueryWrapper<>();
        query.eq("user_id",userId)
                .eq("absolute_position", 1);

        UserAddress userAddress1 = userAddressMapper.selectOne(query);
        userAddress1.setAbsolutePosition(0);

        userAddressMapper.updateById(userAddress1);

        UserAddress userAddress2 = userAddressMapper.selectById(id);
        userAddress2.setAbsolutePosition(1);

        return userAddressMapper.updateById(userAddress2) == 1;
    }

    @Override
    public UserAddress getAbsoluteAddress(Integer userId) {

        QueryWrapper<UserAddress> query = new QueryWrapper<>();
        query.eq("user_id",userId)
                .eq("absolute_position", 1);

        return userAddressMapper.selectOne(query);
    }

    @Override
    public boolean deleteById(Integer userId, Integer addressId) {
        QueryWrapper<UserAddress> query = new QueryWrapper<>();
        query.eq("user_id",userId)
                .eq("id", addressId);

        return userAddressMapper.delete(query)==1;
    }
}
